import { SkillCategoryModel } from '../../models/SkillCategory'

export default defineEventHandler(async (event) => {
  try {
    const { id } = getRouterParams(event)
    const body = await readBody(event)
    
    if (!id) {
      return {
        success: false,
        message: '分类ID不能为空',
        data: null
      }
    }
    
    const updateData: any = {}
    if (body.name !== undefined) updateData.name = body.name
    if (body.description !== undefined) updateData.description = body.description
    if (body.color !== undefined) updateData.color = body.color
    if (body.icon !== undefined) updateData.icon = body.icon
    
    const success = await SkillCategoryModel.update(parseInt(id), updateData)
    
    if (!success) {
      return {
        success: false,
        message: '分类不存在或更新失败',
        data: null
      }
    }
    
    return {
      success: true,
      message: '更新技能分类成功',
      data: { id: parseInt(id) }
    }
  } catch (error: any) {
    console.error('更新技能分类失败:', error)
    
    return {
      success: false,
      message: `更新技能分类失败: ${error.message}`,
      data: null
    }
  }
})